home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / menuscripter / sources / msaecopy.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.7 KB  |  125 lines

  1. // MSAECopy.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. #include "MSAECopy.h"
  8.  
  9. #include "MSAEUtils.h"
  10. #include "MSWindow.h"        // for DPtrFromWindowPtr()
  11.  
  12. #include "MSAESelect.h"
  13.  
  14. #include <Scrap.h>
  15.  
  16.  
  17. #pragma segment AppleEvent
  18.  
  19. // Handle a copy to scrap e.g 'copy last word of document 1'
  20. // Note that 'copy last word of document 1 to end of document 2' is a kAEClone event
  21.      
  22. pascal OSErr    DoCopy(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  23. {
  24. #ifdef __MWERKS__
  25.     #pragma unused (reply, refcon)
  26. #endif
  27.  
  28.     AEDesc        directObj = {typeNull, NULL};
  29.     TextToken    aTextToken;
  30.     short        ignore;
  31.     OSErr        err;
  32.  
  33.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  34.     // If we get an error here it just means that they haven't supplied a reference to
  35.     // an object to copy - so copy the current section instead.
  36.     
  37.     if (directObj.descriptorType != typeNull)
  38.         err = CopyDesc(&directObj);
  39.     else
  40.     {            // Just copy the selection of the front window
  41.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  42.         if (noErr != err) goto done;
  43.         
  44.         err = CopyTextToken(&aTextToken);
  45.     }
  46.  
  47. done:    
  48.     (void)AEDisposeDesc(&directObj);
  49.         
  50.     return(err);
  51. } // DoCopy
  52.  
  53.  
  54. OSErr    CopyTextToken(TextToken* theToken)
  55. {
  56.     WindowPtr        aWindow;
  57.     DPtr            docPtr;
  58.     OSErr            err;
  59.     
  60.     aWindow = theToken->tokenWindow;
  61.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  62.     
  63.     if (! aWindow || ! docPtr)
  64.         return(errAENoSuchObject);
  65.  
  66.                     // Set this tokens selection
  67.     err = SelectTextToken(theToken);
  68.     if (noErr != err) goto done;
  69.  
  70.     err = (OSErr)ZeroScrap();
  71.     TECopy(docPtr->theText);     
  72.     
  73. done:
  74.     return(err);
  75. }
  76.  
  77. OSErr    CopyTextDesc(AEDesc* textDesc)
  78. {
  79.     TextToken        aTextToken;
  80.     Size            actualSize;
  81.     OSErr            err;
  82.  
  83.     if (typeMyText != textDesc->descriptorType)
  84.         return(errAETypeError);
  85.         
  86.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  87.  
  88.     err = CopyTextToken(&aTextToken);
  89.     
  90.     return(err);
  91. }
  92.  
  93. OSErr    CopyDesc(AEDesc* aDesc)
  94. {
  95.     AEDesc        copyDesc = {typeNull, NULL},
  96.                 textDesc = {typeNull, NULL};
  97.     OSErr        err;
  98.     
  99.     if (typeObjectSpecifier == aDesc->descriptorType)
  100.         err = AEResolve(aDesc, kAEIDoMinimum, ©Desc);
  101.     else if (typeNull != aDesc->descriptorType)
  102.         err = AEDuplicateDesc(aDesc, ©Desc);
  103.         
  104.     if (noErr != err) goto done;
  105.     
  106.     switch (copyDesc.descriptorType)
  107.     {
  108.         case typeAEList:
  109.             err = errAETypeError;
  110.             // We can't handle copying more than one item to the scrap
  111.             break;
  112.             
  113.         default:
  114.             err = AECoerceDesc(©Desc, typeMyText, &textDesc);
  115.             if (noErr != err) goto done;
  116.             err = CopyTextDesc(&textDesc);
  117.     }
  118.     
  119. done:
  120.     (void)AEDisposeDesc(©Desc);
  121.     (void)AEDisposeDesc(&textDesc);
  122.     
  123.     return(err);
  124. }
  125.